package com.airlines.crewassignment.controller.crewlocation;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.user.User;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.crewassignment.entity.crewlocation.CrewAbsent;
import com.airlines.crewassignment.service.crewlocation.CrewAbsentService;

/**
 * @author xianghaini
 * @date 2018/07/04 16:23:40
 * @company xxxx Airlines
 * @description  不在位信息
 */
@Controller
@RequestMapping("/crewAbsent")
public class CrewAbsentController {

    private static final String PREFIX = "/crewlocation";

    @Autowired
    CrewAbsentService crewAbsentService;

    /**
     * 进入页面
     * @return
     */
    @RequestMapping(value = "", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:crewAbsent:list")
    String toCrewAbsentPage(Model model,String moduleFlag){
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/crewAbsentList";
    }

    /**
    * 多条件模糊 查询并且分页显示
    * @param crewAbsent
    * @param offset
    * @param limit
    * @return
    */
    @RequestMapping(value = "/list")
    @ResponseBody
   // @RequiresPermissions("crewlocation:crewAbsent:list")
    public Page list(CrewAbsent crewAbsent, String offset, String limit) {
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = crewAbsentService.list(crewAbsent, page);
        return page;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:crewAbsent:add")
    String add(Model model,String moduleFlag){
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/crewAbsentAdd";
    }

    /**
     * 保存
     * @param crewAbsent
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method={ RequestMethod.POST})
   // @RequiresPermissions("crewlocation:crewAbsent:add")
    public R save(CrewAbsent crewAbsent, HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        crewAbsent.setOperator(user.getWorkcode());
        if(crewAbsentService.insert(crewAbsent) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:crewAbsent:edit")
    String edit(Model model,Integer id){
        CrewAbsent crewAbsent = crewAbsentService.get(id);
        model.addAttribute("crewAbsent", crewAbsent);
        return PREFIX + "/crewAbsentEdit";
    }

    /**
     * 修改
     * @param crewAbsent
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method={ RequestMethod.POST})
   // @RequiresPermissions("crewlocation:crewAbsent:edit")
    public R update( CrewAbsent crewAbsent,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        crewAbsent.setOperator(user.getWorkcode());
        if(crewAbsentService.update(crewAbsent)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入查看页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/view", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:crewAbsent:list")
    String view(Model model,Integer id){
        CrewAbsent crewAbsent = crewAbsentService.get(id);
        model.addAttribute("crewAbsent", crewAbsent);
        model.addAttribute("isReadonly", 1);
        return PREFIX + "/crewAbsentEdit";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method={ RequestMethod.POST})
   // @RequiresPermissions("crewlocation:crewAbsent:delete")
    public R delete( Integer id){
        if(crewAbsentService.delete(id)>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 检查代码是否存在
     * @param crewAbsent
     * @return
     */
    @RequestMapping("check")
    @ResponseBody
    public R check(CrewAbsent crewAbsent){
        if(StringUtils.isBlank(crewAbsent.getFromDate()) || StringUtils.isBlank(crewAbsent.getToDate())){
            return null;
        }
        List<CrewAbsent> crewAbsents = crewAbsentService.list(crewAbsent);
        return R.ok().put("valid",crewAbsents.size() == 0);
    }
}
